<template>
  <el-tree
    :data="data"
    :props="defaultProps"
    @node-click="handleNodeClick"
  ></el-tree>
</template>
<script>
  import {m} from "@/store/yg/common/local";
  import {ajax} from "@/store/yg/common/utils";

  export default {
    data() {
      return {
        data: [],
        defaultProps: {
          children: "amoebaSubjectNodes",
          label: "label"
        }
      };
    },
    mounted() {
      this.queryAmoebaSubject();
    },
    methods: {
      handleNodeClick(data) {
        console.log(data);
      },
      sortData(data) {
        for (const j of data) {
          j.label = j.amoebaSubject.no + " " + j.amoebaSubject.name;
          if (
            Object.prototype.toString.call(j.amoebaSubjectNodes) ===
            "[object Array]"
          ) {
            this.sortData(j.amoebaSubjectNodes);
          }
        }
        return data;
      },
      //请求数据
      async queryAmoebaSubject() {
        ajax("queryAmoebaSubject", {}).then(
          res => {
            if (res.code == 200) {
              console.log(res.data);
              this.data = this.sortData(res.data);
            } else {
              if (res.message) {
                this.$message.error(res.message);
              } else {
                this.$message.error("程序异常");
              }
            }
          },
          error => {
            console.log(message);
            this.$message.error("初始化数据失败");
          }
        );
      }
    }
  };
</script>

<style>
  .mail {
    text-align: left;
    padding: 0 20px;
  }

  .subject_all {
    min-height: 360px;
  }
</style>
